package com.jasiri.project.utils.event.track

import androidx.room.ColumnInfo
import androidx.room.Dao
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query


@Entity(tableName = "track_events")
class TrackEvent(
    @PrimaryKey(autoGenerate = true) val id: Int = 0,
    @ColumnInfo(name = "event_time") val eventTime: Long, // 时间戳
    @ColumnInfo(name = "event_type") val eventType: String, // 事件类型
    @ColumnInfo(name = "remark") val remark: String?, // 备注信息
    @ColumnInfo(name = "uploaded") val uploaded: Boolean = false // 是否已上报
){
    constructor(type: String, remark: String?): this(
        eventType = type,
        remark = remark,
        eventTime = System.currentTimeMillis(),
        uploaded = false
    )
}

@Dao
interface TrackEventDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(event: TrackEvent)

    @Query("SELECT * FROM track_events WHERE uploaded = 0")
    suspend fun getUnsentEvents(): List<TrackEvent>

    @Query("UPDATE track_events SET uploaded = 1 WHERE id IN (:ids)")
    suspend fun markAsUploaded(ids: List<Long>)

    @Query("DELETE FROM track_events WHERE uploaded = 1 AND id IN (:ids)")
    suspend fun deleteUploadedEvents(ids: List<Long>)

    @Query("DELETE FROM track_events WHERE uploaded = 1")
    suspend fun clearUploadedEvents()
}